package com.hafu.view.ShangPin;
import com.hafu.model.SpInfo;
import com.hafu.service.SpInfoService;
import com.hafu.util.StringUtil;
import java.awt.*;
import java.awt.event.*;
import java.math.BigDecimal;
import javax.swing.*;
import javax.swing.GroupLayout;
public class AddShangPinFrm extends JFrame {
    private SpInfoService spInfoService = new SpInfoService();

    public AddShangPinFrm() {
        initComponents();
    }

    private void button1(ActionEvent e) {
        String name = textField1.getText();
        if (StringUtil.isEmpty(name)) {
            JOptionPane.showMessageDialog(null, "商品名不能为空");
            return;
        }
        if (StringUtil.isEmpty(textField2.getText())) {
            JOptionPane.showMessageDialog(null, "商品进价不能为空");
            return;
        }
        if (StringUtil.isEmpty(textField3.getText())) {
            JOptionPane.showMessageDialog(null, "商品售价不能为空");
            return;
        }
        BigDecimal price = new BigDecimal(textField2.getText());
        BigDecimal sellPrice = new BigDecimal(textField3.getText());
        SpInfo spInfo = new SpInfo(name, price, sellPrice);
        Boolean aBoolean = spInfoService.addSpInfo(spInfo);
        if (aBoolean) {
            JOptionPane.showMessageDialog(null, "添加成功");
            this.dispose();
        } else {
            JOptionPane.showMessageDialog(null, "添加失败");
        }
    }

    private void initComponents() {
        label1 = new JLabel();
        label2 = new JLabel();
        label3 = new JLabel();
        button1 = new JButton();
        textField1 = new JTextField();
        textField2 = new JTextField();
        textField3 = new JTextField();
        setTitle("\u6dfb\u52a0\u5546\u54c1");
        Container contentPane = getContentPane();
        label1.setText("\u5546\u54c1\u540d\u5b57");
        label2.setText("\u5546\u54c1\u8fdb\u4ef7");
        label3.setText("\u5546\u54c1\u552e\u4ef7");
        button1.setText("\u63d0\u4ea4");
        button1.addActionListener(e -> button1(e));
        GroupLayout contentPaneLayout = new GroupLayout(contentPane);
        contentPane.setLayout(contentPaneLayout);
        contentPaneLayout.setHorizontalGroup(
                contentPaneLayout.createParallelGroup()
                        .addGroup(contentPaneLayout.createSequentialGroup()
                                .addGroup(contentPaneLayout.createParallelGroup()
                                        .addGroup(contentPaneLayout.createSequentialGroup()
                                                .addGap(122, 122, 122)
                                                .addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.LEADING, false)
                                                        .addGroup(contentPaneLayout.createSequentialGroup()
                                                                .addComponent(label3)
                                                                .addGap(91, 91, 91)
                                                                .addComponent(textField3, GroupLayout.DEFAULT_SIZE, 171, Short.MAX_VALUE))
                                                        .addGroup(contentPaneLayout.createSequentialGroup()
                                                                .addGroup(contentPaneLayout.createParallelGroup()
                                                                        .addComponent(label1)
                                                                        .addComponent(label2))
                                                                .addGap(83, 83, 83)
                                                                .addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.LEADING, false)
                                                                        .addComponent(textField1, GroupLayout.DEFAULT_SIZE, 179, Short.MAX_VALUE)
                                                                        .addComponent(textField2, GroupLayout.DEFAULT_SIZE, 179, Short.MAX_VALUE)))))
                                        .addGroup(contentPaneLayout.createSequentialGroup()
                                                .addGap(247, 247, 247)
                                                .addComponent(button1, GroupLayout.PREFERRED_SIZE, 130, GroupLayout.PREFERRED_SIZE)))
                                .addContainerGap(308, Short.MAX_VALUE))
        );
        contentPaneLayout.setVerticalGroup(
                contentPaneLayout.createParallelGroup()
                        .addGroup(contentPaneLayout.createSequentialGroup()
                                .addGap(71, 71, 71)
                                .addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                                        .addComponent(label1)
                                        .addComponent(textField1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
                                .addGap(51, 51, 51)
                                .addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                                        .addComponent(label2)
                                        .addComponent(textField2, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
                                .addGap(38, 38, 38)
                                .addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                                        .addComponent(label3)
                                        .addComponent(textField3, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
                                .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED, 56, Short.MAX_VALUE)
                                .addComponent(button1)
                                .addGap(88, 88, 88))
        );
        pack();
        setLocationRelativeTo(getOwner());
    }

    private JLabel label1;
    private JLabel label2;
    private JLabel label3;
    private JButton button1;
    private JTextField textField1;
    private JTextField textField2;
    private JTextField textField3;
}